﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace SBPweb.PROvibe.Shared.Library.Base
{

    public static class INameableExtensions
    {

        #region Contains

        public static bool ContainsName(this IEnumerable<INameable> element, string name)
        {
            return ContainsName<INameable>(element, name);
        }

        public static bool ContainsName<TSource>(this IEnumerable<TSource> element, string name)
            where TSource : INameable
        {
            return element.Any(x => x.Name == name);
        }

        #endregion

        #region First

        public static INameable FirstByName(this IEnumerable<INameable> element, string name)
        {
            return FirstByName<INameable>(element, name);
        }

        public static TResult FirstByName<TResult>(this IEnumerable<INameable> element, string name)
            where TResult : INameable
        {
            return FirstByName<INameable, TResult>(element, name);
        }

        public static TResult FirstByName<TSource, TResult>(this IEnumerable<TSource> element, string name)
            where TSource : INameable
            where TResult : TSource
        {
            return (TResult)element.FirstOrDefault(x => x.Name == name);
        }

        #endregion

        #region Where

        public static IEnumerable<INameable> WhereNameIs(this IEnumerable<INameable> element, string name)
        {
            return WhereNameIs<INameable>(element, name);
        }

        public static IEnumerable<TResult> WhereNameIs<TResult>(this IEnumerable<INameable> element, string name)
            where TResult : INameable
        {
            return WhereNameIs<INameable, TResult>(element, name);
        }

        public static IEnumerable<TResult> WhereNameIs<TSource, TResult>(this IEnumerable<TSource> element, string name)
            where TSource : INameable
            where TResult : TSource
        {
            return element.Where(x => x.Name == name).Cast<TResult>().ToList();
        }

        #endregion

    }

}